*Set up
clear all
set memory 5g

set more off

use "\\ead02\ead_uquam\Localization\NAICS6_panel\restat_final.dta", clear

cd "\\ead02\ead_uquam\Localization\restat_results"


*TSET FUNCTION;

destring naics, replace
destring oecd80, replace

xtset naics year, delta(1)


//Pre-estimation testing for Lewbel

*regression of the endogeneous variable (transportation cost) on all other exogeneous variables

quietly xi: reg lnav_klems ln_empl lnherfent lnm_emp nmulti1 nfown lnnrs lnpee lnifqh3shr lnrdl m_asiashr m_oecdshr m_naftashr x_asiashr x_oecdshr x_naftashr lnl_idist_n5 lnl_odist_n5 lndistn5 i.year i.naics
estat hettest

*predict residuals for suspected endogenous variables
predict yhat, xb

gen resid=yhat-lnav_klems_resid
gen resid2_av=resid^2

quietly xi: reg lnl_odist_n5 lnav_klems ln_empl lnherfent lnm_emp nmulti1 nfown lnnrs lnpee lnifqh3shr lnrdl m_asiashr m_oecdshr m_naftashr x_asiashr x_oecdshr x_naftashr lnl_idist_n5  lndistn5 i.year i.naics
estat hettest

predict yhat_on, xb

gen resid_od=yhat_on-lnl_odist_n5
gen resid2_od=resid_od^2


quietly xi: reg lnl_idist_n5 lnav_klems ln_empl lnherfent lnm_emp nmulti1 nfown lnnrs lnpee lnifqh3shr lnrdl m_asiashr m_oecdshr m_naftashr x_asiashr x_oecdshr x_naftashr  lnl_odist_n5 lndistn5 i.year i.naics
estat hettest

predict yhat_in, xb

gen resid_id=yhat_in-lnl_idist_n5
gen resid2_id=resid_id^2


quietly xi: reg m_asiashr lnav_klems ln_empl lnherfent lnm_emp nmulti1 nfown lnnrs lnpee lnifqh3shr lnrdl m_oecdshr m_naftashr x_asiashr x_oecdshr x_naftashr  lnl_idist_n5 lnl_odist_n5 lndistn5 i.year i.naics
estat hettest

predict yhat_masia, xb

gen resid_masia=yhat_masia-m_asiashr
gen resid2_masia=resid_id^2


quietly xi: reg m_oecdshr lnav_klems ln_empl lnherfent lnm_emp nmulti1 nfown lnnrs lnpee lnifqh3shr lnrdl m_asiashr m_naftashr x_asiashr x_oecdshr x_naftashr  lnl_idist_n5 lnl_odist_n5 lndistn5 i.year i.naics
estat hettest

predict yhat_moecd, xb

gen resid_moecd=yhat_moecd-m_oecdshr
gen resid2_moecd=resid_id^2


quietly xi: reg m_naftashr lnav_klems ln_empl lnherfent lnm_emp nmulti1 nfown lnnrs lnpee lnifqh3shr lnrdl m_asiashr m_oecdshr x_asiashr x_oecdshr x_naftashr  lnl_idist_n5 lnl_odist_n5 lndistn5 i.year i.naics
estat hettest

predict yhat_mnafta, xb

gen resid_mnafta=yhat_mnafta-m_naftashr
gen resid2_mnafta=resid_id^2


quietly xi: reg x_oecdshr lnav_klems ln_empl lnherfent lnm_emp nmulti1 nfown lnnrs lnpee lnifqh3shr lnrdl m_asiashr m_oecdshr m_naftashr x_asiashr x_naftashr  lnl_idist_n5 lnl_odist_n5 lndistn5 i.year i.naics
estat hettest

predict yhat_xoecd, xb

gen resid_xoecd=yhat_xoecd-x_oecdshr
gen resid2_xoecd=resid_id^2


quietly xi: reg x_naftashr lnav_klems ln_empl lnherfent lnm_emp nmulti1 nfown lnnrs lnpee lnifqh3shr lnrdl m_asiashr m_oecdshr m_naftashr x_asiashr x_oecdshr lnl_idist_n5 lnl_odist_n5 lndistn5 i.year i.naics
estat hettest

predict yhat_xnafta, xb

gen resid_xnafta=yhat_xnafta-x_naftashr
gen resid2_xnafta=resid_id^2


quietly xi: reg x_asiashr lnav_klems ln_empl lnherfent lnm_emp nmulti1 nfown lnnrs lnpee lnifqh3shr lnrdl m_asiashr m_oecdshr m_naftashr x_naftashr x_oecdshr lnl_idist_n5 lnl_odist_n5 lndistn5 i.year i.naics
estat hettest

predict yhat_xasia, xb

gen resid_xasia=yhat_xasia-x_asiashr
gen resid2_xasia=resid_id^2

*Test correlation of transportations cost, input/output distance and export/import shares with residuals square
pwcorr lnav_klems_resid resid2_av, star(0.01)
pwcorr lnl_odist_n5 resid2_od, star(0.01)
pwcorr lnl_idist_n5 resid2_id, star(0.01)
pwcorr m_asiashr resid2_masia, star(0.01)
pwcorr m_oecdshr resid2_xoecd, star(0.01)
pwcorr m_naftashr resid2_xnafta, star(0.01)
pwcorr x_oecdshr resid2_xoecd, star(0.01)
pwcorr x_naftashr resid2_xnafta, star(0.01)
pwcorr x_asiashr resid2_xasia, star(0.01)


/*Table 7: Endogeneity concerns for equation (1) */

//Additional variable generation

*US instrument
generate lnav_klemsus = ln(av_klemsus)

*with IO variables
gen id = lnl_idist_n5
gen od = lnl_odist_n5

set more off

set matsize 10000

*Generate variable to exclude industries with high nafta trade (x + m) over the study period

gen temp = x_naftashr + m_naftashr
qui bys naics: egen xm_nafta = mean(temp)
drop temp

summ xm_nafta, detail 

	gen nafta90=r(p90)


//2SLS and Lewbel estimates

xi: xtreg  ln_cdf50 lnav_klems_resid ln_empl lnherfent lnm_emp nmulti1 nfown lnnrs lnpee lnifqh3shr lnrdl m_asiashr m_oecdshr m_naftashr x_asiashr x_oecdshr x_naftashr lnl_idist_n5 lnl_odist_n5 lndistn5 i.year, fe cluster(naics)
outreg2 using Lewbel_2SLS_rev2, ctitle("Model: Base")  sideway alpha (0.01,0.05,0.10) symbol(a,b,c) stats(coef aster se) noparen excel e(all) replace

xi: xtivreg2 ln_cdf50 ln_empl lnherfent lnm_emp nmulti1 nfown lnnrs lnpee lnifqh3shr lnrdl m_asiashr m_oecdshr m_naftashr x_asiashr x_oecdshr x_naftashr lnl_idist_n5 lnl_odist_n5 lndistn5 i.year (lnav_klems_resid = lnav_klemsus) , fe first cluster(naics)
outreg2 using Lewbel_2SLS_rev2, ctitle("Model: 2SLS")  sideway alpha (0.01,0.05,0.10) symbol(a,b,c) stats(coef aster se) noparen excel e(all) append

*With high nafta trade industries excluded
xi: xtivreg2 ln_cdf50 ln_empl lnherfent lnm_emp nmulti1 nfown lnnrs lnpee lnifqh3shr lnrdl m_asiashr m_oecdshr m_naftashr x_asiashr x_oecdshr x_naftashr lnl_idist_n5 lnl_odist_n5 lndistn5 i.year (lnav_klems_resid = lnav_klemsus) if xm_nafta<nafta90, fe first cluster(naics)
outreg2 using Lewbel_2SLS_rev2, ctitle("2SLS - nafta p90")  sideway alpha (0.01,0.05,0.10) symbol(a,b,c) stats(coef aster se) noparen excel e(all) append


*Demean data 

keep ln_cdf50 lnav_klems lnav_klemsus lnav_klems_resid ln_empl lnherfent lnm_emp nmulti1 nfown lnnrs lnpee lnifqh3shr lnrdl m_asiashr m_oecdshr m_naftashr x_asiashr x_oecdshr x_naftashr lnl_idist_n5 lnl_odist_n5 lndistn5 id od year naics
xtdata, fe clear

gen lnav_klemsr=lnav_klems_resid


//Lewbel

xi: ivreg2h  ln_cdf50 ln_empl lnherfent lnm_emp nmulti1 nfown lnnrs lnpee lnifqh3shr lnrdl m_oecdshr x_asiashr x_oecdshr x_naftashr lndistn5 (lnav_klemsr id od m_asiashr m_naftashr = lnav_klemsus) i.year , cluster(naics) partial(ln_empl lnherfent lnm_emp nmulti1 nfown lnnrs lnpee lnifqh3shr lnrdl m_oecdshr x_asiashr x_oecdshr x_naftashr lndistn5)
outreg2 using Lewbel_2SLS_rev2, ctitle("Model Lewbel 1")  sideway alpha (0.01,0.05,0.10) symbol(a,b,c) stats(coef aster se) noparen excel e(all) append

xi: ivreg2h  ln_cdf50 ln_empl lnherfent lnm_emp nmulti1 nfown lnnrs lnpee lnifqh3shr lnrdl  lndistn5 (lnav_klemsr id od m_asiashr m_oecdshr m_naftashr x_asiashr x_oecdshr x_naftashr = lnav_klemsus) i.year , cluster(naics) partial(ln_empl lnherfent lnm_emp nmulti1 nfown lnnrs lnpee lnifqh3shr lnrdl lndistn5)
outreg2 using Lewbel_2SLS_rev2, ctitle("Model Lewbel 2")  sideway alpha (0.01,0.05,0.10) symbol(a,b,c) stats(coef aster se) noparen excel e(all) append







